/**
 * Created by 叶子 on 2017/8/13.
 */
import React, {Component} from "react";
import {hashHistory, IndexRedirect, Route, Router} from "react-router";
import App from "../App";
import Page from "../components/Page";
import Login from "../components/pages/Login";
import NotFound from "../components/pages/NotFound";
import IndexPage from "@/components/pages/IndexPage";
import FoodMenu from "@/components/pages/FoodMenu.js";
import ManagerUser from "@/components/pages/ManagerUser";
import AddFood from "@/components/pages/AddFood";
import OrderList from "@/components/pages/OrderList";
import TableOrRoomList from "@/components/pages/TableOrRoomList";
import ModifyRestaurant from "@/components/pages/ModifyRestaurant";
import WaiterRate from "@/components/pages/WaiterRate";
import WaiterScoreStatistic from "@/components/pages/WaiterScoreStatistic";
import MemberStatistic from "@/components/pages/MemberStatistic";
// import AddMember from '@/components/pages/AddMember';
// import FoodStatistic from '@/components/pages/FoodStatistic';
import SetMeal from '@/components/pages/SetMeal';
import SelectFoodList from "@/components/pages/SelectFoodList"
import AddMember from "@/components/pages/AddMember";
import FoodStatistic from "@/components/pages/FoodStatistic";
import LogOrderRecordList from "@/components/pages/LogOrderRecordList";


export default class CRouter extends Component {
    requireAuth = (permission, component) => {
        const {store} = this.props;
        const {auth} = store.getState().httpData;
        if (!auth || !auth.data.permissions.includes(permission)) window.location.hash = '/404';
        return component;
    };
    requireLogin = (nextState, replace) => {
        const {store} = this.props;
        const {auth} = store.getState().httpData;
        if (!auth || !auth.data) {
            //只有登录后才能访问，避免用户直接访问app
            replace({pathname: '/'})
        }
    };

    render() {
        return (
            <Router history={hashHistory}>
                <Route path={'/'} components={Page}>
                    <IndexRedirect to="login" />
                    <Route path={'login'} components={Login} />
                    <Route path={'app'} component={App}>
                        <Route path={'index'} component={IndexPage}/>
                        <Route path={'managerUser'} component={ManagerUser}/>
                        <Route path={'modifyRestaurant'} component={ModifyRestaurant} />
                        <Route path={'food/foodMenu'} component={FoodMenu} />
                        <Route path={'food/addFood'} component={AddFood} />
                        <Route path={'food/foodstatistic'} component={FoodStatistic}/>
                        <Route path={'orderList'} component={OrderList} />
                        <Route path={'tableOrRoom'} component={TableOrRoomList}/>
                        <Route path={'waiter/waiterRate'} component={WaiterRate} />
                        <Route path={'waiter/waiterScoreStatistic'} component={WaiterScoreStatistic}/>
                        <Route path={'member/addmember'} component={AddMember}/>
                        <Route path={'member/memberStatistic'} component={MemberStatistic}/>
                        <Route path={"setmeal/setmealList"} component={SetMeal} />
                        <Route path={"setmeal/selectFoodList"} component={SelectFoodList} />
                        <Route path={'log/order'} component={LogOrderRecordList}/>
                    </Route>
                </Route>
                <Route path={'*'} component={NotFound} />
            </Router>
        )
    }
}